﻿using System.Linq;
using System.Text.RegularExpressions;

namespace Foundation.ExtensionMethods
{
    /// <summary>
    /// Extension methods for strings.
    /// </summary>
    public static class StringExtensions
    {
        public static readonly Regex DefaultWholeWordRegex = new Regex(@"\b\w+\b", RegexOptions.Compiled);

        /// <summary>
        /// Gets the current word.
        /// </summary>
        /// <param name="text">The text.</param>
        /// <param name="wholeWordRegex">The whole word regex.</param>
        /// <param name="currentOffset">The current offset.</param>
        /// <returns></returns>
        public static Match GetCurrentWord(this string text, Regex wholeWordRegex, int currentOffset)
        {
            var matches = wholeWordRegex.Matches(text).Cast<Match>();
            var match = matches.FirstOrDefault(m => m.Index <= currentOffset && currentOffset <= m.Index + m.Length);
            return match;
        }
    }
}
